<template>
  <echartBox title="供应统计" width="651px" height="427px">
    <div class="echart-show" ref="chart"></div>
  </echartBox>
</template>

<script>
import * as echarts from "echarts";
import echartBox from "../echart-box.vue";

export default {
  name: "Page1ItemBox2",
  components: {
    echartBox,
  },
  props: {
    chartData: {
      type: Array,
      required: true,
      default: () => [],
    },
  },
  methods: {
    // 初始化图表的统一方法名
    initChart() {
      this.buildSupplyStatChart();
    },
    // 获取当前月份
    getCurrentMonth() {
      const currentMonth = new Date().getMonth() + 1; // getMonth() 返回 0-11，所以要加1
      return `${currentMonth}月`;
    },
    // 构建供应统计图表
    buildSupplyStatChart() {
      let _labelList = this.chartData.map((item) => item.name);
      let _valueList = this.chartData.map((item) => item.value);
      const currentMonth = this.getCurrentMonth();
      const offsetX = 12;
      const offsetY = 5;
      // 绘制左侧面
      const CubeLeft = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          const xAxisPoint = shape.xAxisPoint;
          const c0 = [shape.x, shape.y];
          const c1 = [shape.x - offsetX, shape.y - offsetY];
          const c2 = [xAxisPoint[0] - offsetX, xAxisPoint[1] - offsetY];
          const c3 = [xAxisPoint[0], xAxisPoint[1]];
          ctx
            .moveTo(c0[0], c0[1])
            ?.lineTo(c1[0], c1[1])
            .lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .closePath();
        },
      });
      // 绘制右侧面
      const CubeRight = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          const xAxisPoint = shape.xAxisPoint;
          const c1 = [shape.x, shape.y];
          const c2 = [xAxisPoint[0], xAxisPoint[1]];
          const c3 = [xAxisPoint[0] + offsetX, xAxisPoint[1] - offsetY];
          const c4 = [shape.x + offsetX, shape.y - offsetY];
          ctx
            .moveTo(c1[0], c1[1])
            ?.lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .lineTo(c4[0], c4[1])
            .closePath();
        },
      });
      // 绘制顶面
      const CubeTop = echarts.graphic.extendShape({
        shape: {
          x: 0,
          y: 0,
        },
        buildPath: function (ctx, shape) {
          const c1 = [shape.x, shape.y];
          const c2 = [shape.x + offsetX, shape.y - offsetY];
          const c3 = [shape.x, shape.y - offsetY * 2];
          const c4 = [shape.x - offsetX, shape.y - offsetY];
          ctx
            .moveTo(c1[0], c1[1])
            ?.lineTo(c2[0], c2[1])
            .lineTo(c3[0], c3[1])
            .lineTo(c4[0], c4[1])
            .closePath();
        },
      });
      // 注册三个面图形
      echarts.graphic.registerShape("CubeLeft", CubeLeft);
      echarts.graphic.registerShape("CubeRight", CubeRight);
      echarts.graphic.registerShape("CubeTop", CubeTop);
      let chartContainer = this.$refs.chart;
      let myChart = echarts.init(chartContainer);
      const option = {
        color: ["#53edff"],
        backgroundColor: "transparent",
        tooltip: {
          show: true,
          trigger: "axis", //axis , item
          backgroundColor: "RGBA(0, 49, 85, 1)",
          borderColor: "rgba(0, 151, 251, 1)",
          borderWidth: 1,
          borderRadius: 0,
          textStyle: {
            color: "#BCE9FC",
            fontSize: 16,
            align: "left",
          },
        },
        grid: {
          left: "8%",
          right: "1%",
          bottom: "1%",
          top: "16%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          data: _labelList,
          axisLine: {
            show: true,
            lineStyle: {
              color: "rgba(255, 255, 255, 0.3)",
            },
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: "#fff",
            fontSize: 15,
          },
        },
        yAxis: {
          name: "单位：万/m³",
          nameTextStyle: {
            color: "#fff",
          },
          type: "value",
          max: 1000,
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: "rgba(255, 255, 255, 0.1)",
              type: "dashed",
            },
          },
          axisLabel: {
            color: "#fff",
            fontSize: 15,
          },
        },
        series: [
          {
            type: "custom",
            renderItem: (params, api) => {
              const location = api.coord([api.value(0), api.value(1)]);
              const dataIndex = params.dataIndex;
              const monthName = _labelList[dataIndex];
              const isCurrentMonth = monthName === currentMonth;

              // 设置颜色：当前月份使用蓝色系高亮，其他月份使用原色
              const leftColors = isCurrentMonth
                ? [
                    { offset: 0, color: "#00bfff" }, // 深天蓝色高亮
                    { offset: 0.5, color: "#1e90ff" }, // 道奇蓝
                    { offset: 1, color: "#4169e1" }, // 皇家蓝
                  ]
                : [
                    { offset: 0, color: "#3299b3aa" },
                    { offset: 0.5, color: "#1f6887aa" },
                    { offset: 1, color: "#021a3daa" },
                  ];

              const rightColors = isCurrentMonth
                ? [
                    { offset: 0, color: "#87ceeb" }, // 天空蓝高亮
                    { offset: 0.5, color: "#00bfff" }, // 深天蓝
                    { offset: 1, color: "#4169e1" }, // 皇家蓝
                  ]
                : [
                    { offset: 0, color: "#45c9dfaa" },
                    { offset: 0.5, color: "#1f6887aa" },
                    { offset: 1, color: "#021a3daa" },
                  ];

              const topColor = isCurrentMonth ? "#87cefa" : "#53edffaa";

              return {
                type: "group",
                children: [
                  {
                    type: "CubeLeft",
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: api.coord([api.value(0), 0]),
                    },
                    style: {
                      fill: new echarts.graphic.LinearGradient(
                        0,
                        0,
                        0,
                        1,
                        leftColors
                      ),
                    },
                  },
                  {
                    type: "CubeRight",
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: api.coord([api.value(0), 0]),
                    },
                    style: {
                      fill: new echarts.graphic.LinearGradient(
                        0,
                        0,
                        0,
                        1,
                        rightColors
                      ),
                    },
                  },
                  {
                    type: "CubeTop",
                    shape: {
                      api,
                      xValue: api.value(0),
                      yValue: api.value(1),
                      x: location[0],
                      y: location[1],
                      xAxisPoint: api.coord([api.value(0), 0]),
                    },
                    style: {
                      fill: topColor,
                    },
                  },
                ],
              };
            },
            data: _valueList,
          },
        ],
      };
      myChart.setOption(option);
      myChart.on("mouseover", {}, function (mParams) {
        console.log(mParams)
          option.series = [
            {
              type: "custom",
              renderItem: (params, api) => {
                const location = api.coord([api.value(0), api.value(1)]);
                const dataIndex = params.dataIndex;
                const monthName = _labelList[dataIndex];
                const currentMonth = mParams.dataIndex + 1 + "月";
                const isCurrentMonth = monthName === currentMonth;

                // 设置颜色：当前月份使用蓝色系高亮，其他月份使用原色
                const leftColors = isCurrentMonth
                  ? [
                      { offset: 0, color: "#00bfff" }, // 深天蓝色高亮
                      { offset: 0.5, color: "#1e90ff" }, // 道奇蓝
                      { offset: 1, color: "#4169e1" }, // 皇家蓝
                    ]
                  : [
                      { offset: 0, color: "#3299b3aa" },
                      { offset: 0.5, color: "#1f6887aa" },
                      { offset: 1, color: "#021a3daa" },
                    ];

                const rightColors = isCurrentMonth
                  ? [
                      { offset: 0, color: "#87ceeb" }, // 天空蓝高亮
                      { offset: 0.5, color: "#00bfff" }, // 深天蓝
                      { offset: 1, color: "#4169e1" }, // 皇家蓝
                    ]
                  : [
                      { offset: 0, color: "#45c9dfaa" },
                      { offset: 0.5, color: "#1f6887aa" },
                      { offset: 1, color: "#021a3daa" },
                    ];

                const topColor = isCurrentMonth ? "#87cefa" : "#53edffaa";

                return {
                  type: "group",
                  children: [
                    {
                      type: "CubeLeft",
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        xAxisPoint: api.coord([api.value(0), 0]),
                      },
                      style: {
                        fill: new echarts.graphic.LinearGradient(
                          0,
                          0,
                          0,
                          1,
                          leftColors
                        ),
                      },
                    },
                    {
                      type: "CubeRight",
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        xAxisPoint: api.coord([api.value(0), 0]),
                      },
                      style: {
                        fill: new echarts.graphic.LinearGradient(
                          0,
                          0,
                          0,
                          1,
                          rightColors
                        ),
                      },
                    },
                    {
                      type: "CubeTop",
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        xAxisPoint: api.coord([api.value(0), 0]),
                      },
                      style: {
                        fill: topColor,
                      },
                    },
                  ],
                };
              },
              data: _valueList,
            },
          ];
        myChart.setOption(option);
      });
      myChart.on("mouseout", {}, function (params) {
        option.series = [{
              type: "custom",
              renderItem: (params, api) => {
                const location = api.coord([api.value(0), api.value(1)]);
                const dataIndex = params.dataIndex;

                // 设置颜色：当前月份使用蓝色系高亮，其他月份使用原色
                const leftColors =  [
                      { offset: 0, color: "#3299b3aa" },
                      { offset: 0.5, color: "#1f6887aa" },
                      { offset: 1, color: "#021a3daa" },
                    ];

                const rightColors =  [
                      { offset: 0, color: "#45c9dfaa" },
                      { offset: 0.5, color: "#1f6887aa" },
                      { offset: 1, color: "#021a3daa" },
                    ];

                const topColor = "#53edffaa";

                return {
                  type: "group",
                  children: [
                    {
                      type: "CubeLeft",
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        xAxisPoint: api.coord([api.value(0), 0]),
                      },
                      style: {
                        fill: new echarts.graphic.LinearGradient(
                          0,
                          0,
                          0,
                          1,
                          leftColors
                        ),
                      },
                    },
                    {
                      type: "CubeRight",
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        xAxisPoint: api.coord([api.value(0), 0]),
                      },
                      style: {
                        fill: new echarts.graphic.LinearGradient(
                          0,
                          0,
                          0,
                          1,
                          rightColors
                        ),
                      },
                    },
                    {
                      type: "CubeTop",
                      shape: {
                        api,
                        xValue: api.value(0),
                        yValue: api.value(1),
                        x: location[0],
                        y: location[1],
                        xAxisPoint: api.coord([api.value(0), 0]),
                      },
                      style: {
                        fill: topColor,
                      },
                    },
                  ],
                };
              },
              data: _valueList,
            },]
      });
    },
  },
};
</script>

<style scoped lang="scss">
.echart-show {
  width: 100%;
  height: 100%;
}
</style>
